<html>

<head>
	<title>Web activity manager - Inserisci log </title>
	<link href="menu/menu.css" rel="stylesheet" type="text/css"/>
	<script src="xmlLoader.js" type="text/javascript"></script>
	<script src="menu_and_sessiondata.js" type="text/javascript"></script>
	<script type="text/javascript">
		
		function init(){
		
			loadSessionData();
			generateMenu();
			loadTasks();
		}
		
		function currentDate(){
		
			// FONTE SCRIPT: http://stackoverflow.com/questions/12409299/how-to-get-current-formatted-date-dd-mm-yyyy-in-javascript-and-append-it-to-an-i
		
			var today = new Date();
			var dd = today.getDate();
			var mm = today.getMonth()+1; // January is 0!

			var yyyy = today.getFullYear();
			if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} var today = dd+'/'+mm+'/'+yyyy;
			document.getElementById("data").value = today;
	
			
		}
		
		function loadTasks(){
			
			var link="/sintesiTuttiTask";
			var tasksXML=loadXMLDoc(link);
			
			var tasks=tasksXML.getElementsByTagName("elemento");
			var i;
			var innertasks="";
			
			for(i=0;i<tasks.length;i++){
						id_task=tasksXML.getElementsByTagName("id_task")[i].childNodes[0].nodeValue;
						name=tasksXML.getElementsByTagName("name")[i].childNodes[0].nodeValue;
						
						innertasks+=('<option value="'+id_task+'">'+name+'</option>');
						 
			}
			document.getElementById("selecttasks").innerHTML=innertasks;	
			
		}
		
		function isDate(){
					var stringa=document.getElementById("data").value;
					var espressione = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
					 if (!espressione.test(stringa))  {
						alert("Formato non valido. Il formato corretto e': gg/mm/aaaa");
						document.getElementById("data").value="";
						return false;
					  } else {
						 // Recupera dalla stringa i campi anno, mese e giorno
							anno = parseInt(stringa.substr(6),10);
							mese = parseInt(stringa.substr(3, 2),10);
							giorno = parseInt(stringa.substr(0, 2),10);
 
							// Crea la nuova data
							var data=new Date(anno, mese-1, giorno);
						 
							// Controlla che i parametri della data siano
							//  gli stessi che abbiamo impostato
							if (data.getFullYear()==anno &&
								  data.getMonth()+1==mese &&
								  data.getDate()==giorno){
							  return true;
							} else {
								
							  alert("Data non valida");
							document.getElementById("data").value="";
						}
					 }
					}
					  
			
		
	</script>
	<link href="style/style.css" rel="stylesheet" type="text/css"/>
</head>

<body onload="init()">
	<div class = "header">

		<div class = "headerban">
			<h1><center> WEB ACTIVITY MANAGER </center></h1>
		</div>
		
		<div class = "session_data">
			<table>
				<tr>
					<td><b>Utente: </b></td>
					<td><div id = "current_user"></div>
					</td>
				</tr>
				<tr>
					<td>
						<b>Ruolo: </b></td>
					<td><div id = "current_role" ></div>
					</td>
				</tr>
				<tr>
					<td>
						<b>Progetto: </b></td>
					<td><div id = "current_project"></div>
					</td>
				</tr>
				<tr>
					<td>
						<a href="http://localhost:4444/logout"><input type="button" value="logout"/></a>
					</td>
				</tr>
			</table>
		</div>
	</div>
	
	<!-- MENU -->
	<div class = "menu">
		<ul id="menu">
		</ul>
	</div>
	<br>
	<br>
	
	
	<div class = "funct_name">
		<h2><center> Inserisci un nuovo log </center></h2>
	</div>
	
	<!-- INSERIMENTO LOG -->
	<form name="inserimentolog" action="http://localhost:4444/inserisciLog">
		<fieldset>
			<legend> Inserisci un nuovo log </legend>
				<table class="insertlog">
					<tr>
						<td><label for="task"> Task </label> </td>
						<td>
							<select id="selecttasks" name="task" required>
							</select>
						</td>
					</tr>
					
					<tr>
						<td><label for="data"> Data </label> </td>
						<td><input type="text" name="data" onChange="isDate();" required id="data" /> </td>
						<td><input id="datacorr" type="button" value="Data corrente" onclick="currentDate();" /></td>
					</tr>
					<tr>
						<td><label for="description"> Descrizione </label> </td>
						<td><input type="textarea" name="description" id="description" /> </td>
					</tr>
					<tr>
						<td><label for="ore"> Ore lavorate</label> </td>
						<td><input type="text" name="ore" required id="ore" /> </td>
					</tr>
				</table>
				<input type="submit" value="Inserisci log"/>
			</fieldset>
		</form>
</body>

</html>